package controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.CTPhieuNhap;

public class CTPhieuNhapController {

	public static List<CTPhieuNhap> getCtpn(int idpn) {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return null;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM cthdnhap WHERE mahdn=" + idpn + ";";
			ResultSet rs = stmt.executeQuery(sql);
			CTPhieuNhap pn;
			List<CTPhieuNhap> list = new ArrayList<>();
			while (rs.next()) {
				pn = new CTPhieuNhap();
				pn.setMaHDPN(idpn);
				pn.setSoLuong(rs.getInt("soluong"));
				MatHangController m = new MatHangController();
				int id = rs.getInt("maMH");
				pn.setMatHang(m.getMatHang(id));
				list.add(pn);

			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

	public static void main(String[] args) {
		CTPhieuNhapController c = new CTPhieuNhapController();
		System.out.println(c.getCtpn(1));
	}
}
